/**
 * 
 */
package com.alstudio.core.voip;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.module.voip.CallActivity;
import com.alstudio.ui.module.voip.PhoneRecordActivity;
import com.alstudio.utils.android.notification.ALNotification;
import com.alstudio.utils.android.notification.ALNotification.ALNotificationType;
import com.alstudio.utils.android.notification.ALNotificationCenterManager;
import com.alstudio.utils.android.notification.ALNotificationIdConfig;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class ALVoipCallNotificationCenter {

	private ALNotificationCenterManager mNotificationCenterManager;
	private Context mContext;

	/**
	 * 
	 */
	public ALVoipCallNotificationCenter(Context context,
			ALNotificationCenterManager notificationCenterManager) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.mNotificationCenterManager = notificationCenterManager;
	}

	public void showInCallNotification(ALMessage callInfo) {
		if (callInfo == null)
			return;

		ALNotification n = buildCallingNotification();
		User u = (User) callInfo.getData();
		n.setTicker(ALLocalEnv.getLocalEnv()
				.getString(R.string.TxtCallingTitle));
		n.setContentTitle(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallingTitle));
		String msg = ALLocalEnv.getLocalEnv().getString(
				R.string.TxtMissCallingInfo);
		msg = String.format(msg, u.getNick());
		n.setContentText(msg);
		n.setId(ALNotificationIdConfig.IN_CALL_NOTIFICATION_ID);
		mNotificationCenterManager.showNotification(n);
	}

	public void showOutGoingCallNotification(ALMessage callInfo) {
		if (callInfo == null)
			return;

		ALNotification n = buildCallingNotification();
		User u = (User) callInfo.getData();
		n.setTicker(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallOutGoingTitle));
		n.setContentTitle(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallOutGoingTitle));
		n.setContentText(ALLocalEnv.getLocalEnv()
				.getString(R.string.TxtCalling));
		n.setId(ALNotificationIdConfig.OUT_GOING_CALL_NOTIFICATION_ID);
		mNotificationCenterManager.showNotification(n);
	}

	public void showInCommingCallNotification(ALMessage callInfo) {
		if (callInfo == null)
			return;

		ALNotification n = buildCallingNotification();
		User u = (User) callInfo.getData();
		n.setTicker(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallIncommingTitle));
		n.setContentTitle(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallIncommingTitle));
		String msg = ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallIncommingInfo);
		msg = String.format(msg, u.getNick());
		n.setContentText(msg);
		n.setId(ALNotificationIdConfig.INCOMMING_CALL_NOTIFICATION_ID);
		mNotificationCenterManager.showNotification(n);
	}

	/**
	 * 来电提醒
	 * 
	 * @param callInfo
	 */
	public void showInCommingCallNoticeNotification(ALMessage callInfo) {
		if (callInfo == null)
			return;

		// ALNotification n =
		// buildCallingNotification(PhoneRecordActivity.class);
		// User u = (User) callInfo.getData();
		// n.setOngoing(false);
		// n.setTicker("來電提醒");
		// n.setContentTitle(u.getNick() + "剛剛打電話找你");
		// n.setContentText("点击返回");
		// n.setId(INCOMMING_CALL_NOTICE_NOTIFICATION_ID);
		// mNotificationCenterManager.showNotification(n);

		ALNotification n = buildCallingNotification(PhoneRecordActivity.class);
		// User u = (User) callInfo.getData();
		n.setTicker(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtMissCallNoticeTitle));
		n.setContentTitle(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtMissCallNoticeTitle));
		int count = ALLocalEnv.getLocalEnv().getDatabase()
				.getUnReadCallHistoryByMissStype(false);
		String msg = ALLocalEnv.getLocalEnv().getString(
				R.string.TxtMissCallNoticeInfo);
		msg = String.format(msg, count);
		n.setContentText(msg);
		n.setId(ALNotificationIdConfig.INCOMMING_CALL_NOTICE_NOTIFICATION_ID);
		mNotificationCenterManager.showNotification(n);
	}

	/**
	 * 顯示未接來電
	 * 
	 * @param callInfo
	 */
	public void showMissedCallNotification(ALMessage callInfo) {
		if (callInfo == null)
			return;

		ALNotification n = buildCallingNotification(PhoneRecordActivity.class);
		// User u = (User) callInfo.getData();
		n.setTicker(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtMissCallTitle));
		n.setContentTitle(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtMissCallTitle));
		int count = ALLocalEnv.getLocalEnv().getDatabase()
				.getUnReadCallHistoryByMissStype(false);
		String msg = ALLocalEnv.getLocalEnv().getString(
				R.string.TxtMissCallInfo);
		msg = String.format(msg, count);
		n.setContentText(msg);
		n.setId(ALNotificationIdConfig.INCOMMING_MISSED_CALL_NOTICE_NOTIFICATION_ID);
		mNotificationCenterManager.showNotification(n);
	}

	public void showNewCallChatMsgNotification(ALMessage callInfo, int chatCount) {
		if (callInfo == null)
			return;

		ALNotification n = buildCallingNotification();
		User u = (User) callInfo.getData();
		n.setTicker(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallChatImageTitle));
		n.setContentTitle(ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallChatImageTitle));
		String msg = ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallChatImageInfo);
		msg = String.format(msg, chatCount);
		n.setContentText(msg);
		n.setId(ALNotificationIdConfig.IN_CALL_CHAT_MSG_NOTIFICATION_ID);
		mNotificationCenterManager.showNotification(n);
	}

	public void removeNewCallChatMsgNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.IN_CALL_CHAT_MSG_NOTIFICATION_ID);
	}

	public void removeInCallNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.IN_CALL_NOTIFICATION_ID);
	}

	public void removeOutGoingCallNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.OUT_GOING_CALL_NOTIFICATION_ID);
	}

	public void removeInCommingCallNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.INCOMMING_CALL_NOTIFICATION_ID);

	}

	public void removeInCommingCallNoticeNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.INCOMMING_CALL_NOTICE_NOTIFICATION_ID);
	}

	public void removeInCommingMissedCallNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.INCOMMING_MISSED_CALL_NOTICE_NOTIFICATION_ID);
	}

	/**
	 * 構造返回到通話介面的通知
	 * 
	 * @return
	 */
	private ALNotification buildCallingNotification() {
		ALNotification n = new ALNotification(mContext,
				ALNotificationType.AL_NOTIFICATION_TYPE_NORMAL, "正在通话");
		n.setAutoCancel(false);
		n.setOngoing(true);
		n.setWhen(System.currentTimeMillis());
		n.setSmallIcon(R.drawable.login_bt_on);

		Intent intent = new Intent(mContext, CallActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		PendingIntent contentIntent = PendingIntent.getActivity(mContext,
				ALNotificationIdConfig.IN_CALL_NOTIFICATION_ID, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);

		n.setContentIntent(contentIntent);
		n.setId(ALNotificationIdConfig.IN_CALL_NOTIFICATION_ID);

		n.setShowing(true);
		return n;
	}

	private ALNotification buildCallingNotification(Class<?> cls) {
		ALNotification n = new ALNotification(mContext,
				ALNotificationType.AL_NOTIFICATION_TYPE_NORMAL, "正在通话");
		n.setAutoCancel(false);
		n.setOngoing(true);
		n.setWhen(System.currentTimeMillis());
		n.setSmallIcon(R.drawable.login_bt_on);

		Intent intent = new Intent(mContext, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		PendingIntent contentIntent = PendingIntent.getActivity(mContext,
				ALNotificationIdConfig.IN_CALL_NOTIFICATION_ID, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);

		n.setContentIntent(contentIntent);

		n.setShowing(true);
		return n;
	}

}
